﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Configuration;

namespace FiftyNine.Ag.AzureDevelopmentStorageProxy
{
    class Program
    {
        static HttpListener _listener;
        static string BLOB_ADDRESS = ConfigurationManager.AppSettings["blobAddress"].ToLower();
        static string TABLE_ADDRESS = ConfigurationManager.AppSettings["tableAddress"].ToLower();
        static string QUEUE_ADRESS = ConfigurationManager.AppSettings["queueAddress"].ToLower();

        static void Main(string[] args)
        {
            _listener = new HttpListener();
            _listener.Prefixes.Add(BLOB_ADDRESS);
            _listener.Prefixes.Add(TABLE_ADDRESS);
            _listener.Prefixes.Add(QUEUE_ADRESS);
            _listener.Start();
            Console.WriteLine("Listening for dev storage connections...");
            _listener.BeginGetContext(new AsyncCallback(RequestStarted), _listener);
            Console.ReadKey();
        }

        static void RequestStarted(IAsyncResult iar)
        {
            _listener.BeginGetContext(new AsyncCallback(RequestStarted), _listener);

            HttpListenerContext ctx = ((HttpListener)iar.AsyncState).EndGetContext(iar);
            HttpListenerRequest request = ctx.Request;
            HttpListenerResponse response = ctx.Response;

            RequestHandler.HandleRequest(request, response);
        }
    }
}
